package com.egene.tw.blue;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;

/**
 * Created by wzx on 2018/8/1.
 */

public class ShakeUtils {
    private static Vibrator mVibrator;
    private static MediaPlayer mMediaPlayer;
    private static boolean isShakeing;

    public static void shake(Context context) {
        if(isShakeing){
            return;
        }
        isShakeing = true;
        //shake
        if(mVibrator == null){
            mVibrator = (Vibrator) context.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
        }
        if (mVibrator != null) {
            mVibrator.vibrate(new long[]{500, 1500}, 1);
        }

        //rang
        if(mMediaPlayer == null){
            Uri mediaUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            mMediaPlayer = MediaPlayer.create(context.getApplicationContext(), mediaUri);
            mMediaPlayer.setLooping(true);
        }
        if(mMediaPlayer != null){
            mMediaPlayer.start();
        }
    }

    public static void stopShake() {
        isShakeing = false;
        if (mVibrator != null) {
            mVibrator.cancel();
        }
        mVibrator = null;
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }
        mMediaPlayer = null;
    }


}
